package com.raycloud.erp.omll.provider.config;

import com.raycloud.secret_api.api.SecretRequest;
import com.taobao.diamond.manager.DiamondManager;
import com.taobao.diamond.manager.ManagerListener;
import com.taobao.diamond.manager.impl.DefaultDiamondManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import javax.security.auth.callback.PasswordCallback;
import java.util.concurrent.Executor;

/**
 * 数据库密码坐标回传实现
 * @author liumingjian
 * @date 2019/1/25
 **/
@Slf4j
public class DiamondPasswordCallback extends PasswordCallback {

    /**
     * diamondCoord 坐标
     */
    private String diamondCoord;

    private SecretRequest secretRequest;

    private DiamondManager diamondManager;

    private int retryTime = 5;

    private Object locker = new Object();


    /**
     * Construct a {@code PasswordCallback} with a prompt
     * and a boolean specifying whether the password should be displayed
     * as it is being typed.
     * <p>
     * <p>
     *
     * @param prompt the prompt used to request the password. <p>
     * @param echoOn true if the password should be displayed
     *               as it is being typed.
     * @throws IllegalArgumentException if {@code prompt} is null or
     *                                  if {@code prompt} has a length of 0.
     */
    public DiamondPasswordCallback(String prompt, boolean echoOn) {
        super(prompt, echoOn);
    }

    public DiamondPasswordCallback(){
        this("diamond password callback", false);
    }

    public String getDiamondCoord() {
        return diamondCoord;
    }

    public void setDiamondCoord(String diamondCoord) {
        this.diamondCoord = diamondCoord;
    }

    public SecretRequest getSecretRequest() {
        return secretRequest;
    }

    public void setSecretRequest(SecretRequest secretRequest) {
        this.secretRequest = secretRequest;
    }

    @Override
    public char[] getPassword() {
        Assert.isTrue(StringUtils.isNotEmpty(diamondCoord), "请输入密码坐标");
        Assert.isTrue(secretRequest != null, "请设置secretRequest的dubbo引用");
        String password0;
        if(diamondManager == null){
            synchronized (locker) {
                if(diamondManager == null) {
                    diamondManager = createDiamondManager();
                }
            }
        }
        try {
            password0 = getPassword0(diamondManager);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            password0 = null;
        }
        if(password0 != null){
            return password0.toCharArray();
        }
        return new char[0];
    }

    private String getPassword0(DiamondManager diamondManager) throws Exception {
        String password = diamondManager.getAvailableConfigureInfomation(5000);
        if(null == password){
            return null;
        }
        return secretRequest.decode(password);
    }

    DefaultDiamondManager createDiamondManager(){
        String[] paris = diamondCoord.split("\\.");

        return new DefaultDiamondManager(paris[0],paris[1], new ManagerListenerImpl());
    }

    class ManagerListenerImpl implements ManagerListener {

        @Override
        public Executor getExecutor() {
            return null;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
            // do nothing
        }
    }
}
